package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.mapper.mwsreport.IMwsRemovalOrderDetailMapper;
import com.yunchang.springboot.models.mwsreportbean.MwsRemovalOrderDetail;
import com.yunchang.springboot.models.mwsreportbean.MwsRemovalOrderDetailDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:46
 * @description:
 */
@Component
public class RemovalOrderDetailDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Autowired
    IMwsRemovalOrderDetailMapper mwsRemovalOrderDetailMapper;

    /**
     * 移除订单详情 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area, String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        int requestDateIndex = Integer.valueOf(String.valueOf(titleMap.get("request-date")));
        int orderIdIndex = Integer.valueOf(String.valueOf(titleMap.get("order-id")));
        int orderTypeIndex = Integer.valueOf(String.valueOf(titleMap.get("order-type")));
        int orderStatusIndex = Integer.valueOf(String.valueOf(titleMap.get("order-status")));
        int lastUpdatedDateIndex = Integer.valueOf(String.valueOf(titleMap.get("last-updated-date")));
        int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
        int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
        int dispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("disposition")));
        int requestedQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("requested-quantity")));
        int cancelledQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("cancelled-quantity")));
        int disposedQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("disposed-quantity")));
        int shippedQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("shipped-quantity")));
        int inProcessQuantityIndex = Integer.valueOf(String.valueOf(titleMap.get("in-process-quantity")));
        int removalFeeIndex = Integer.valueOf(String.valueOf(titleMap.get("removal-fee")));
        int currencyIndex = Integer.valueOf(String.valueOf(titleMap.get("currency")));
        int serviceSpeedIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("service-speed")) {
            serviceSpeedIndex = Integer.valueOf(String.valueOf(titleMap.get("service-speed")));
        }
        List<Object> insertList = new ArrayList<>();
        for (int i = 1; i < lists.size(); i++) {
            MwsRemovalOrderDetailDO mwsRemovalOrderDetail = new MwsRemovalOrderDetailDO();
            List<String> row = lists.get(i);
            mwsRemovalOrderDetail.setOrderId(row.get(orderIdIndex));
            mwsRemovalOrderDetail.setSku(row.get(skuIndex));
            mwsRemovalOrderDetail.setFnsku(row.get(fnskuIndex));
            mwsRemovalOrderDetail.setDisposition(row.get(dispositionIndex));
            mwsRemovalOrderDetail.setLastUpdatedDate(DateUtil.ISO8601ToGMT(row.get(lastUpdatedDateIndex)));
            MwsRemovalOrderDetailDO oneInfoByOrderIdAndSkuAndFnSkuAndDisposition = mwsRemovalOrderDetailMapper.getOneInfoByOrderIdAndSkuAndFnSkuAndDisposition(mwsRemovalOrderDetail.getOrderId(), mwsRemovalOrderDetail.getSku(),
                    mwsRemovalOrderDetail.getFnsku(), mwsRemovalOrderDetail.getDisposition());
            //如果存在该条数据
            if (null != oneInfoByOrderIdAndSkuAndFnSkuAndDisposition && null != oneInfoByOrderIdAndSkuAndFnSkuAndDisposition.getId()) {
                //如果最后更新时间和本次获取的最后更新时间一致
                if (oneInfoByOrderIdAndSkuAndFnSkuAndDisposition.getLastUpdatedDate().equals(mwsRemovalOrderDetail.getLastUpdatedDate())) {
                    continue;
                }
            }
            mwsRemovalOrderDetail.setSellerId(sellerId);
            mwsRemovalOrderDetail.setArea(area);
            mwsRemovalOrderDetail.setRequestDate(DateUtil.ISO8601ToGMT(row.get(requestDateIndex)));
            mwsRemovalOrderDetail.setOrderType(row.get(orderTypeIndex));
            mwsRemovalOrderDetail.setOrderStatus(row.get(orderStatusIndex));
            mwsRemovalOrderDetail.setRequestedQuantity(ToolUtil.valueOfInteger(row.get(requestedQuantityIndex)));
            mwsRemovalOrderDetail.setCancelledQuantity(ToolUtil.valueOfInteger(row.get(cancelledQuantityIndex)));
            mwsRemovalOrderDetail.setDisposedQuantity(ToolUtil.valueOfInteger(row.get(disposedQuantityIndex)));
            mwsRemovalOrderDetail.setShippedQuantity(ToolUtil.valueOfInteger(row.get(shippedQuantityIndex)));
            mwsRemovalOrderDetail.setInProcessQuantity(ToolUtil.valueOfInteger(row.get(inProcessQuantityIndex)));
            mwsRemovalOrderDetail.setRemovalFee(new BigDecimal(String.valueOf(row.get(removalFeeIndex))));
            mwsRemovalOrderDetail.setCurrency(row.get(currencyIndex));
            mwsRemovalOrderDetail.setCreateTime(DateUtil.getDate());
            mwsRemovalOrderDetail.setUpdateTime(DateUtil.getDate());
            if (serviceSpeedIndex != WRONG_NUMBER_SIGN) {
                mwsRemovalOrderDetail.setServiceSpeed(row.get(serviceSpeedIndex));
            }
            if (null == oneInfoByOrderIdAndSkuAndFnSkuAndDisposition || null == oneInfoByOrderIdAndSkuAndFnSkuAndDisposition.getId()) {
                mwsRemovalOrderDetail.setId(null);
                mwsRemovalOrderDetail.setFinanceStatus(1);
            } else {
                mwsRemovalOrderDetail.setId(oneInfoByOrderIdAndSkuAndFnSkuAndDisposition.getId());
            }
            insertList.add(mwsRemovalOrderDetail);
        }
        return insertList;
    }
}
